
import htmllib,formatter,re
from common import *
    

#   this class in charge of catching the link of a page
class LinkParser(htmllib.HTMLParser):
    def __init__(self,flag = NORMAL_SITE):
        fmt = formatter.AbstractFormatter(formatter.NullWriter())
        htmllib.HTMLParser.__init__(self,fmt)
        self.hrefsList = []
        self.hostPath =''
        # 0 :google 10 :normal url
        self.flag = NORMAL_SITE
        
    #    set the flag for the parser, parsing GOOGLE or other site
    def setFlag(self,flag = NORMAL_SITE):
        self.flag = flag
        
    #    set the hostpath for the url
    def setHostPath(self,hostPath):
        self.hostPath = hostPath    
        
    #    get the <a> tag and add the href's value to self.hrefs
    def start_a(self, attrs):
        #    for the google result
        if self.flag == GOOGLE:
            if ("class",'l') in attrs:
                self.getLinks(attrs)
        elif self.flag == NORMAL_SITE:
            self.getLinks(attrs)
            
    #    get the <a> tag and add the href's value to self.hrefs
    def do_img(self,attrs):
        if self.flag != GOOGLE:
            self.getLinks(attrs)
    
    def searchAttrValue(self,attrs,target = 'href'):
        for items in attrs:
            if items[0] == target:
                return items[1]
        return False
    
    #    clear all the record in the object
    def clearHrefsList(self):
        self.hrefsList = []
        
    #    parse the wanted links with in attr
    def getLinks(self,attrs):
        targetAttribute = []
        if FLAG_PARSE_HREF == True:
            targetAttribute.append('href')
        if FLAG_PARSE_SRC == True:
            targetAttribute.append('src')
        for target in targetAttribute:
            httpMatch = re.compile('^http://+')
            h = self.searchAttrValue(attrs,target)
            
            if h != False:
                if httpMatch.match(h) == None: # for relative url
                    h = self.hostPath +'/' +h
                if len(h.split('/')) < WIERED_SLASHES:
                    self.hrefsList.append(h)
     
